"""
绘制轮廓的外接矩形
"""
import cv2
import numpy as np

img = cv2.imread('../data/cloud.png')
cv2.imshow('img',img)

# 灰度化
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 二值化
t,binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)
cv2.imshow('binary',binary)
# 查找轮廓
cnts,hie = cv2.findContours(binary,
                            cv2.RETR_LIST, # 查找所有轮廓
                            cv2.CHAIN_APPROX_NONE)

# 将查找到的轮廓，交给boundingrect ---> x,y,w,h
x,y,w,h = cv2.boundingRect(cnts[0])
# 根据x,y,w,h --->确定出矩形的4个点
points = np.array([[[x,y]], # 左上角
                   [[x,y+h]], # 左下角
                   [[x+w,y+h]], #右下角
                   [[x+w,y]]]) # 右上角

# 调用drawcontours绘制出来
cv2.drawContours(img,
                 [points],
                 0,
                 (0,0,255),
                 2)

cv2.imshow('res',img)

cv2.waitKey()
cv2.destroyAllWindows()